package com.lzy.cloud.base.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.base.dto.BaseLicenceDto;
import com.lzy.cloud.base.request.BaseLicenceRequest;
import com.lzy.cloud.base.response.BaseLicenceResponse;
import com.lzy.cloud.base.service.LicenceService;
import com.lzy.framework.common.dtos.LicenseDto;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Base;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目表
 */
@RestController
@RequestMapping(value = "/licence")
@Slf4j
public class LicenceController {

    @Autowired
    private LicenceService licenceService;

    /**
     * 列表
     * @param request
     * @return
     */
    @RequestMapping(value = "/list")
    public Result list(@RequestBody BaseLicenceRequest request){
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<BaseLicenceDto> pageInfo = licenceService.getList(pager);
        List<BaseLicenceDto> list = pageInfo.getList();
        if (pageInfo != null && pageInfo.getList() != null) {
            return new Result(pager, DozerUtils.maps(list, BaseLicenceResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 获取详情
     * @param id
     * @return
     */
    @RequestMapping(value = "/find/{id}",method = RequestMethod.GET)
    public Result find(@PathVariable Long id){
        return new Result(licenceService.findById(id));
    }

    /**
     * 新增
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody BaseLicenceRequest request){
        Long id = licenceService.insert(DozerUtils.map(request,BaseLicenceDto.class));
        Ensure.that(id>0).isTrue("00030002");
        return new Result(id);
    }

    /**
     * 修改
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody BaseLicenceRequest request){
        int count = licenceService.update(DozerUtils.map(request,BaseLicenceDto.class));
        Ensure.that(count>0).isTrue("00030003");
        return new Result();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.POST)
    public Result delete(@PathVariable Long id){
        int count = licenceService.deleteById(id);
        Ensure.that(count>0).isTrue("00030003");
        return new Result();
    }

    /**
     * 根据sourceId获取营业执照
     * @param sourceId
     * @return
     */
    @RequestMapping(value = "/detail/one/{sourceId}",method = RequestMethod.POST)
    public Result detail(@PathVariable("sourceId") Long sourceId){
        Ensure.that(sourceId).isNotNull("");
        BaseLicenceDto baseLicenceDto = new BaseLicenceDto();
        baseLicenceDto.setSourceId(sourceId);
        baseLicenceDto.setType(1);
        List<BaseLicenceDto> licenceDtoList = licenceService.selectLicence(baseLicenceDto);
        BaseLicenceDto licenceDto = new BaseLicenceDto();
        if(licenceDtoList != null && licenceDtoList.size() >0){
            licenceDto = licenceDtoList.get(0);
        }
        return new Result(licenceDto.getImageUrl());
    }



}
